package cn.zjgsu.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.zjgsu.constant.UserConstant;

import cn.zjgsu.domain.TbUser;

import cn.zjgsu.service.ILoginService;

@Controller
@RequestMapping("/")
public class IndexController {

	@Resource
	private ILoginService loginService;

/*	@Resource
 //private LeaveService leaveService;
*/
	@RequestMapping("/")
	public String index(HttpSession session,HttpServletRequest req) {

		if (session.getAttribute("user") == null)

			return "index";

		else {

			TbUser user = (TbUser) session.getAttribute("user");
			if (UserConstant.ADMIN.equals(user.getRole())) {
				//req.setAttribute("leaves", leaveService.selectCanLeave());
				return "admin/index";

			} else if (UserConstant.TEACHER.equals(user.getRole())) {
				//req.setAttribute("leaves", leaveService.selectNeedLeave());
				return "teacher/index";

			} else {

				return "student/index";

			}
		}
	}

	@RequestMapping("/login")
	public String login(String username, String password, HttpServletRequest req) {

		TbUser user = loginService.selectUser(username, password);

		if (user != null) {

			req.getSession().setAttribute("user", user);

			if (UserConstant.ADMIN.equals(user.getRole())) {
				
				
				//req.setAttribute("leaves", leaveService.selectCanLeave());
				return "admin/index";

			} else if (UserConstant.TEACHER.equals(user.getRole())) {
				
				//req.setAttribute("leaves", leaveService.selectNeedLeave());

				return "teacher/index";

			} else {

				return "student/index";

			}
		} else {

			req.setAttribute("error", "<script>alert('用户名或密码错误')</script>");
			return "index";

		}
	}

	@RequestMapping("/logout")
	public String logout(HttpServletRequest req) {
		req.getSession().removeAttribute("user");
		return "index";
	}

	/*@RequestMapping("/leave")
	public String leave(Leave leave, HttpServletRequest req) {

		leaveService.insertLeave(leave);

		return "redirect:/";
	}

	@RequestMapping("/showleave")
	@ResponseBody
	public String showleave(int id) {

		return leaveService.selectLeave(id);

	}
	
	@RequestMapping("/monleave")
	public String monleave(int id){
		
		leaveService.monleave(id);
		
		return "redirect:/";
	
	}
	
	@RequestMapping("/unmonleave")
	public String unmonleave(int id){
		
		leaveService.unmonleave(id);
		
		return "redirect:/";
	
	}
	
	@RequestMapping("/inleave")
	public String inleave(int id){
		
		leaveService.inleave(id);
		
		return "redirect:/";
	
	}
	

	@RequestMapping("/uninleave")
	public String uninleave(int id){
		
		leaveService.uninleave(id);
		
		return "redirect:/";
	
	}*/

}
